import Vue from 'vue';
import iView from 'iview';
import { router } from './router/index';
import { appRouter } from './router/router';
import store from './store';
import ECharts from 'vue-echarts';
import App from './app.vue';
import '@/locale';
import 'iview/dist/styles/iview.css';
import VueI18n from 'vue-i18n';
import util from './libs/util';
import axios from 'axios';
import Moment from 'moment';
import qs from 'qs';

Vue.use(VueI18n);
Vue.use(iView);
Vue.component('chart', ECharts);

Vue.config.productionTip = false;
Vue.prototype.$mapServer = process.env.NODE_ENV === 'development' ? 'https://yxys.io/hospital' : 'https://yxys.io/hospital';

axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
if (sessionStorage.token || localStorage.token) axios.defaults.headers.common['auth_token'] = sessionStorage.token || localStorage.token;
Vue.prototype.$getRemoteData = (url, options = { method: 'get' }) => {
    return new Promise((resolve, reject) => {
        if (options.method.toLowerCase() === 'get') {
            axios.get(url).then((data) => {
                resolve(data);
            }).catch((e) => {
                // if (e.response && e.response.status === 401) return
                reject(e);
            });
        } else if (options.method.toLowerCase() === 'put') {
            axios.put(url, qs.stringify(options.data)).then((data) => {
                resolve(data);
            }).catch((e) => {
                reject(e);
            });
        } else if (options.method.toLowerCase() === 'delete') {
            axios.delete(url).then((data) => {
                resolve(data);
            }).catch((e) => {
                reject(e);
            });
        } else if (options.method.toLowerCase() === 'post') {
            axios.post(url, qs.stringify(options.data)).then((data) => {
                resolve(data);
            }).catch((e) => {
                reject(e);
            });
        }
    });
};

Vue.prototype.$generateRandom = (type = 'number', range = [0, 1], options = {}) => {
    switch (type) {
        case 'number':
            const decimal = options.decimal || 0;
            return (range[0] + parseFloat(Math.random() * (range[1] - range[0]))).toFixed(decimal);
            break;
        case 'date':
            const start = new Moment(range[0]).unix();
            const end = new Moment(range[1]).unix();
            const delta = start - end;
            return (start.unix() + Math.random() * delta).format('YYYY-MM-dd');
            break;
    }
};

new Vue({
    el: '#app',
    router: router,
    store: store,
    render: h => h(App),
    data: {
        currentPageName: ''
    },
    mounted() {
        this.currentPageName = this.$route.name;
        // 显示打开的页面的列表
        this.$store.commit('setOpenedList');
        this.$store.commit('initCachepage');
        // 权限菜单过滤相关
        this.$store.commit('updateMenulist');
        // iview-admin检查更新
        util.checkUpdate(this);
    },
    created() {
        let tagsList = [];
        appRouter.map((item) => {
            if (item.children.length <= 1) {
                tagsList.push(item.children[0]);
            } else {
                tagsList.push(...item.children);
            }
        });
        this.$store.commit('setTagsList', tagsList);
    }
});
